layui.use(['table', 'jquery', 'laydate'], function () {
    const table = layui.table,
        $ = layui.jquery;

    //显示日期
    showTime('#startDate');
    showTime('#endDate');

    //默认加载时执行
    findAll();
    findCodeByType('signStatus', $("#signStatus"));

    function findAll() {

        let code = $('#code').val();
        let username = $('#username').val();
        let signStatus = $("#signStatus option:selected").val();
        let startDate = $('#startDate').val();
        let endDate = $('#endDate').val();
        let params = {
            code: code,
            username: username,
            signStatus: signStatus,
        };
        if (startDate !== undefined && startDate != null && startDate !== '') {
            params['startDate'] = startDate
        }
        if (endDate !== undefined && endDate != null && endDate !== '') {
            params['endDate'] = endDate
        }

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'signId', width: 80, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'code', width: '10%', title: '用户编号', sort: true}
            , {field: 'username', width: '10%', title: '用户名'}
            , {
                field: 'avatar', width: '10%', title: '头像', templet: function (e) {
                    //判断图片是否为空
                    e.avatar === null || e.avatar === undefined ? e.avatar = '/upload/img_null.png' : e.avatar;
                    return '<img src="' + e.avatar + '" width="80px" height="30px">'
                }
            }
            , {field: 'signTime', width: '20%', title: '签到时间', sort: true}
            , {
                field: 'signStatus', width: '12%', title: '签到状态', templet: e => {
                    return getSignStatus(e.signStatus);
                }
            }
            , {field: 'lateTime', width: '18%', title: '迟到时间', sort: true}
            , {fixed: 'right', width: 165, align: 'center', toolbar: '#operating'}
        ]];


        table.render({
            elem: '#dataList'
            , url: LOCAL_CROSS_URL + "/sign/findAllSignRecord"
            , where: params
            , method: 'get'
            , headers: HEADERS
            , request: REQUEST
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , toolbar: '#toolbar'
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 20, 30, 50]
            , limit: 10
            , title: '签到记录表'
            , cols: cols
        });
    }


    //监听头工具栏事件
    table.on('toolbar(dataList)', function (obj) {
        const checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data;
        switch (obj.event) {
            case 'batchDel':
                if (data.length === 0) {
                    layer.msg('请至少选择一行');
                } else {
                    let ids = [];
                    for (let i = 0; i < data.length; i++) {
                        //获取每一行选中的Id
                        ids += checkStatus.data[i].signId + ',';
                    }
                    //去掉最后一个逗号
                    ids = ids.substring(0, ids.lastIndexOf(','));
                    layer.confirm('真的删除行么', function (index) {
                        layer.close(index);
                        batchDel(ids.toString())
                    });
                }
                break;
        }
    });

    //监听行工具事件
    table.on('tool(dataList)', function (obj) {
        const data = obj.data
            , layEvent = obj.event;
        if (layEvent === 'del') {
            layer.confirm('真的删除行么', function (index) {
                layer.close(index);
                //向服务端发送删除指令
                deleteSignRecord(data.signId);
            });
        } else if (layEvent === 'edit') {
            updateSignRecord(data.signId);
        }
    });


    /**
     * 删除
     * @param id
     */
    function deleteSignRecord(id) {

        let result = commonAjax('/sign/delete', {id: id}, 'get');
        if (result.code === 200) {
            layerMsgDeleteSuccess();
            findAll();
        } else {
            layerMsgDeleteFail();
        }
    }

    /**
     * 批量删除
     */
    function batchDel(ids) {

        let result = commonBatchAjax('/sign/deletes', {ids: ids}, 'post');
        if (result.code === 200) {
            layerMsgDeleteSuccess();
            findAll();
        } else {
            layerMsgDeleteFail();
        }
    }

    /**
     * 修改
     * @param id
     */
    function updateSignRecord(id) {
        sessionStorage.setItem("signRecordId", id);
        commonLayerOpen('修改签到信息', 'signRecordDetails.html');
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll()
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#code').val('');
        $('#username').val('');
        $('#signStatus').val('');
        $('#startDate').val('');
        $('#endDate').val('');
    });
});